import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _classCallCheck from "@babel/runtime/helpers/esm/classCallCheck";
import _createClass from "@babel/runtime/helpers/esm/createClass";
import _inherits from "@babel/runtime/helpers/esm/inherits";
import _createSuper from "@babel/runtime/helpers/esm/createSuper";
import _applyDecoratedDescriptor from "@babel/runtime/helpers/esm/applyDecoratedDescriptor";
import _omit from "lodash/omit";
import _isFunction from "lodash/isFunction";
import _forEach from "lodash/forEach";
import _map from "lodash/map";

var _dec, _dec2, _dec3, _dec4, _dec5, _class, _class2;

/**
 * 带查询的表单 - 就是 动态表单 限制 col = 3,
 * @date 2018/10/24

 */
import React from 'react';
import { Form, Button, Row, Col, Icon } from 'antd';
import { Bind } from 'lodash-decorators';
import intl from "../../../utils/intl";
import { dealObjectProps, preProcessComponentProps, getComponentType, getGetValueFromEventFunc, getGetValuePropFunc } from "../utils";
import { postDynamicFormProcessComponentProps } from "../DynamicForm/utils";
import { searchFormOmitProps } from "../config";
var btnStyle = {
  marginRight: 8
};
var DynamicSearchForm = (_dec = Form.create({
  fieldNameProp: null
}), _dec2 = Bind(), _dec3 = Bind(), _dec4 = Bind(), _dec5 = Bind(), _dec(_class = (_class2 = /*#__PURE__*/function (_React$Component) {
  _inherits(DynamicSearchForm, _React$Component);

  var _super = _createSuper(DynamicSearchForm);

  function DynamicSearchForm(props) {
    var _this;

    _classCallCheck(this, DynamicSearchForm);

    _this = _super.call(this, props); // state

    _this.state = {
      advanceFormActive: false
    };
    return _this;
  }
  /**
   * 渲染查询表单
   * 根据 fields.length 的不同 返回不同的表单
   * <= 3 普通表单
   * <= 5 不同表单 字段固定比例宽
   * > 5 高级表单
   */


  _createClass(DynamicSearchForm, [{
    key: "render",
    value: function render() {
      var _this$props = this.props,
          _this$props$fields = _this$props.fields,
          fields = _this$props$fields === void 0 ? [] : _this$props$fields,
          searchBtnText = _this$props.searchBtnText,
          resetBtnText = _this$props.resetBtnText,
          className = _this$props.className;

      var otherFormProps = _omit(this.props, searchFormOmitProps);

      var buttons = /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Button, {
        key: "search",
        onClick: this.handleSearchBtnClick,
        type: "primary",
        style: btnStyle
      }, searchBtnText || intl.get('cpaas.common.button.search').d('查询')), /*#__PURE__*/React.createElement(Button, {
        key: "reset",
        onClick: this.handleResetBtnClick,
        style: btnStyle
      }, resetBtnText || intl.get('cpaas.common.button.reset').d('重置')));
      var formClassName = "".concat(className || '', " table-list-form");

      if (fields.length <= 3) {
        return /*#__PURE__*/React.createElement(Form, Object.assign({
          layout: "inline",
          className: formClassName
        }, otherFormProps), this.renderFormItems(), /*#__PURE__*/React.createElement(Form.Item, {
          key: "btn"
        }, buttons));
      }

      return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Form, Object.assign({
        className: formClassName
      }, otherFormProps), /*#__PURE__*/React.createElement(Row, null, /*#__PURE__*/React.createElement(Col, {
        span: 18
      }, /*#__PURE__*/React.createElement(Row, null, this.renderFormItems())), /*#__PURE__*/React.createElement(Col, {
        span: 6,
        className: "search-btn-more"
      }, /*#__PURE__*/React.createElement(Form.Item, null, buttons, this.renderAdvanceBtn())))));
    }
  }, {
    key: "componentDidMount",
    value: function componentDidMount() {
      var onRef = this.props.onRef;

      if (_isFunction(onRef)) {
        onRef(this);
      }
    }
    /**
     * 渲染表单字段
     */

  }, {
    key: "renderFormItems",
    value: function renderFormItems() {
      var _this$props2 = this.props,
          fields = _this$props2.fields,
          context = _this$props2.context,
          form = _this$props2.form;

      var contextFields = _map(fields, function (field) {
        var contextField = dealObjectProps(field, context);
        contextField.props = preProcessComponentProps({
          field: contextField,
          context: context
        });
        return contextField;
      });

      _forEach(contextFields, function (field) {
        postDynamicFormProcessComponentProps({
          fields: contextFields,
          field: field,
          dealProps: field.props,
          form: form
        });
      });

      return _map(contextFields, this.renderField);
    }
    /**
     * 根据条件渲染字段
     * 当字段大于3 且 当前表单是高级表单的激活状态时 返回 Col 前三个字段, 否则返回全部字段
     * 当字段小于3时 返回 FormItem
     * @param {object} field - 当前字段
     * @param {number} index - 当前字段是所有字段的第index个字段
     */

  }, {
    key: "renderField",
    value: function renderField(field, index) {
      var _this$props3 = this.props,
          fields = _this$props3.fields,
          form = _this$props3.form;
      var advanceFormActive = this.state.advanceFormActive;
      var ComponentType = getComponentType(field);
      var otherFormItemOptions = {};
      var getValueFromEvent = getGetValueFromEventFunc(field.componentType);
      var getValueProps = getGetValuePropFunc(field);

      if (getValueFromEvent) {
        otherFormItemOptions.getValueFromEvent = getValueFromEvent;
      }

      if (getValueProps) {
        // 不影响存的值, 只影响传递给组件的值
        otherFormItemOptions.getValueProps = getValueProps;
      }

      if (fields.length <= 3) {
        return /*#__PURE__*/React.createElement(Form.Item, {
          label: field.fieldLabel,
          required: field.requiredFlag !== 0
        }, form.getFieldDecorator(field.fieldName, _objectSpread(_objectSpread({}, otherFormItemOptions), {}, {
          rules: [{
            required: field.required,
            message: intl.get('cpaas.common.validation.notNull', {
              name: field.fieldLabel
            }).d("".concat(field.fieldLabel, "\u4E0D\u80FD\u4E3A\u7A7A"))
          }]
        }))(React.createElement(ComponentType, field.props)));
      }

      if (fields.length > 5 && !advanceFormActive && index >= 3) {
        return null;
      }

      return /*#__PURE__*/React.createElement(Col, {
        span: 8
      }, /*#__PURE__*/React.createElement(Form.Item, {
        labelCol: {
          span: 10
        },
        wrapperCol: {
          span: 14
        },
        label: field.fieldLabel,
        required: field.requiredFlag !== 0
      }, form.getFieldDecorator(field.fieldName, _objectSpread(_objectSpread({}, otherFormItemOptions), {}, {
        rules: [{
          required: field.required,
          message: intl.get('cpaas.common.validation.notNull', {
            name: field.fieldLabel
          }).d("".concat(field.fieldLabel, "\u4E0D\u80FD\u4E3A\u7A7A"))
        }]
      }))(React.createElement(ComponentType, field.props))));
    }
    /**
     * 按条件来渲染高级表单按钮
     * this.props.fields.length
     * <=5 不渲染 return null
     * >5 且 this.state.advanceFormActive 为真 渲染收起按钮 返回 收起按钮
     * >5 且 this.state.advanceFormActive 为假 渲染展开按钮 返回 展开按钮
     */

  }, {
    key: "renderAdvanceBtn",
    value: function renderAdvanceBtn() {
      var fields = this.props.fields;
      var advanceFormActive = this.state.advanceFormActive;

      if (fields.length <= 5) {
        return null;
      }

      return advanceFormActive ? /*#__PURE__*/React.createElement("a", {
        onClick: this.handleCloseAdvanceBtn,
        className: "unselect"
      }, intl.get('cpaas.common.button.up').d('收起'), /*#__PURE__*/React.createElement(Icon, {
        type: "up"
      })) : /*#__PURE__*/React.createElement("a", {
        onClick: this.handleExpandAdvanceBtn,
        className: "unselect"
      }, intl.get('cpaas.common.button.expand').d('展开'), /*#__PURE__*/React.createElement(Icon, {
        type: "down"
      }));
    }
    /**
     * 关闭高级表单
     */

  }, {
    key: "handleCloseAdvanceBtn",
    value: function handleCloseAdvanceBtn() {
      this.setState({
        advanceFormActive: false
      });
    }
    /**
     * 展开高级表单
     */

  }, {
    key: "handleExpandAdvanceBtn",
    value: function handleExpandAdvanceBtn() {
      this.setState({
        advanceFormActive: true
      });
    }
    /**
     * 查询按钮点击
     */

  }, {
    key: "handleSearchBtnClick",
    value: function handleSearchBtnClick() {
      var _this$props4 = this.props,
          form = _this$props4.form,
          submitTrigger = _this$props4.submitTrigger;
      return new Promise(function (resolve, reject) {
        if (_isFunction(submitTrigger)) {
          form.validateFields(function (err, fieldsValue) {
            if (err) {
              reject(err);
              return;
            }

            submitTrigger(fieldsValue).then(resolve, reject);
          });
        }
      });
    }
    /**
     * 重置按钮点击
     */

  }, {
    key: "handleResetBtnClick",
    value: function handleResetBtnClick() {
      var _this$props5 = this.props,
          form = _this$props5.form,
          onReset = _this$props5.onReset;
      form.resetFields();

      if (_isFunction(onReset)) {
        onReset();
      }
    }
  }]);

  return DynamicSearchForm;
}(React.Component), (_applyDecoratedDescriptor(_class2.prototype, "handleCloseAdvanceBtn", [_dec2], Object.getOwnPropertyDescriptor(_class2.prototype, "handleCloseAdvanceBtn"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleExpandAdvanceBtn", [_dec3], Object.getOwnPropertyDescriptor(_class2.prototype, "handleExpandAdvanceBtn"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleSearchBtnClick", [_dec4], Object.getOwnPropertyDescriptor(_class2.prototype, "handleSearchBtnClick"), _class2.prototype), _applyDecoratedDescriptor(_class2.prototype, "handleResetBtnClick", [_dec5], Object.getOwnPropertyDescriptor(_class2.prototype, "handleResetBtnClick"), _class2.prototype)), _class2)) || _class);
export { DynamicSearchForm as default };
var exportFuncs = ['submitTrigger'];
var exportFuncsInfo = {
  submitTrigger: {
    descriptionIntlCode: 'cpfm.ui.tpl.form.submit',
    descriptionIntlDefault: intl.get('cpaas.common.button.submit').d('提交')
  }
}; // const internalFuncs = ['lineRemove'];
// const internalFuncsInfo = {
//   lineRemove: {
//     descriptionIntlCode: 'cpfm.ui.tpl.table.internal.lineRemove',
//     descriptionIntlDefault: '删除行',
//     code: 'lineRemove',
//   },
// };
// 暴露出去的方法

DynamicSearchForm.exportFuncs = exportFuncs;
DynamicSearchForm.exportFuncsInfo = exportFuncsInfo; // // 内部方法
// DynamicSearchForm.internalFuncs = internalFuncs;
// DynamicSearchForm.internalFuncsInfo = internalFuncsInfo;